const db = require("../db/index")

// 获取banner图
banner = (req, res) => {
    let sql = "select * from bannerimg"
    let p = new Promise((resolve, reject) => {
        db.query(sql, '', (err, result) => {
            if (err) return reject("获取失败")
            if (result.length > 0) {
                return resolve(result)
            }else{
                reject("没有数据")
            }
        })
    })
    p.then(r => {
        res.send({
            state: 200,
            data: r,
            mess:"成功"
        })
    }, v => {
        res.send({
            state: 401,
            message: v
        })
    })
};

// 获取首页推荐的图片
getRecommend = (req, res) => {
    let sql = "select * from recommend"
    let p = new Promise((resolve, reject) => {
        db.query(sql, '', (err, result) => {
            if (err) return reject("获取失败")
            if (result.length > 0) {
                return resolve(result)
            }else{
                reject("没有数据")
            }
        })
    })
    p.then(r => {
        res.send({
            state: 200,
            message: r
        })
    }, v => {
        res.send({
            state: 401,
            message: v
        })
    })
};


// 上传蛋糕商品的图片
pushCake = (req,res)=>{
    // console.log(req.query)
    let cakeShopobj = req.query
    let sql = "insert into cake set ?"
    let p = new Promise((resolve,reject)=>{
        db.query(sql,cakeShopobj,(err,result)=>{
            if(err) return reject(err)
            if(result.affectedRows==1) return resolve(result)
        })
    })
    
    p.then(r=>{
        res.send({
            code:200,
            message:'成功',
        })
    },v=>{
        res.send({
            code:401,
            message:v,
        })
    })
}

// 获取蛋糕商品的图片
getCakeData = (req,res)=>{

    let sql = "select * from cake";

    let p = new Promise((resolve,reject)=>{
        db.query(sql,"",(err,result)=>{
            if(err) return reject("输入的信息有误")
            if(result.length>0){
                resolve(result)
            }else{
                reject("获取数据失败")
            }
        })
    })

    p.then(r=>{
        res.send({
            code:200,
            mess:'成功',
            data:r
        })
    },v=>{
        res.send({
            code:401,
            mess:r
        })
    })

}



// 上传面包商品的数据
pushBread = (req,res)=>{
    // console.log(req.query)
    let cakeShopobj = req.query
    let sql = "insert into bread set ?"
    let p = new Promise((resolve,reject)=>{
        db.query(sql,cakeShopobj,(err,result)=>{
            if(err) return reject(err)
            if(result.affectedRows==1) return resolve(result)
        })
    })
    
    p.then(r=>{
        res.send({
            code:200,
            message:'成功',
        })
    },v=>{
        res.send({
            code:401,
            message:v,
        })
    })
}


// 获取面包商品的数据
getBreadData = (req,res)=>{

    let sql = "select * from bread";

    let p = new Promise((resolve,reject)=>{
        db.query(sql,"",(err,result)=>{
            if(err) return reject("输入的信息有误")
            if(result.length>0){
                resolve(result)
            }else{
                reject("获取数据失败")
            }
        })
    })

    p.then(r=>{
        res.send({
            code:200,
            mess:'成功',
            data:r
        })
    },v=>{
        res.send({
            code:401,
            mess:r
        })
    })

}


// 上传小食商品的数据
pushSnack = (req,res)=>{
    // console.log(req.query)
    let cakeShopobj = req.query
    let sql = "insert into snack set ?"
    let p = new Promise((resolve,reject)=>{
        db.query(sql,cakeShopobj,(err,result)=>{
            if(err) return reject(err)
            if(result.affectedRows==1) return resolve(result)
        })
    })
    
    p.then(r=>{
        res.send({
            code:200,
            message:'成功',
        })
    },v=>{
        res.send({
            code:401,
            message:v,
        })
    })
}


// 获取小食商品的数据
getSnackData = (req,res)=>{

    let sql = "select * from snack";

    let p = new Promise((resolve,reject)=>{
        db.query(sql,"",(err,result)=>{
            if(err) return reject("输入的信息有误")
            if(result.length>0){
                resolve(result)
            }else{
                reject("获取数据失败")
            }
        })
    })

    p.then(r=>{
        res.send({
            code:200,
            mess:'成功',
            data:r
        })
    },v=>{
        res.send({
            code:401,
            mess:r
        })
    })

}


module.exports = {
    banner,
    pushCake,
    getCakeData,
    getRecommend,
    pushBread,
    getBreadData,
    pushSnack,
    getSnackData
}